package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.RechargeStationDAO;
import com.pojo.RechargeStation;
import com.util.StringUtil;
import com.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 充值点信息表 服务实现类
 * </p>
 *
 * @author ZhangXiaoJun
 * @since 2021-09-01
 */
@Service
public class RechargeStationService extends ServiceImpl<RechargeStationDAO, RechargeStation> {

    @Autowired
    private RechargeStationDAO rechargeStationDAO;

    /**
     * 分页获取充值点信息
     * @param page
     * @param limit
     * @param name
     * @return
     */
    public IPage<RechargeStation> getByPage(Integer page, Integer limit, String name, Integer status){
        QueryWrapper query = new QueryWrapper() ;
        if(status!=null){
            query.eq("status",status); //状态查询条件
        }
        if(StringUtil.isNotNull(name)) {
            query.like("name" , name) ; //名称查询条件
        }

        query.orderByDesc("createTime"); //按创建时间降序排
        Page p = new Page(page, limit);

        IPage<RechargeStation> iPage = rechargeStationDAO.selectPage(p,query);
        return iPage;
    }

    /**
     * 检测充值点名称是否存在
     * @param name
     * @return
     */
    public boolean checkNameRepeat(String name,String id){
        QueryWrapper query = new QueryWrapper() ;
        query.eq("name",name);
        if(ValidateHelper.isNotEmptyString(id)){
            query.ne("id",id); //此条件用于修改时的验证
        }
        long totals = rechargeStationDAO.selectCount(query);
        if(totals>0){
            return true;
        }
        return false;
    }

    /**
     * 检测号码是否存在
     * @param phone
     * @return
     */
    public boolean checkPhoneRepeat(String phone,String id){
        QueryWrapper query = new QueryWrapper() ;
        query.eq("phone",phone);
        if(ValidateHelper.isNotEmptyString(id)){
            query.ne("id",id); //此条件用于修改时的验证
        }
        long totals = rechargeStationDAO.selectCount(query);
        if(totals>0){
            return true;
        }
        return false;
    }



    /**
     * 获取充值点总数
     * @return
     */
    public Long getRechargeCount () {
        QueryWrapper q = new QueryWrapper();
        return rechargeStationDAO.selectCount(q);
    }

}
